package inventoryServe.controller;


import inventoryServe.po.*;

import inventoryServe.service.InventoryService;

import org.springframework.beans.factory.annotation.Autowired;
import inventoryServe.utils.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-06-22
 */
@Controller
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    // 获取库存列表数据
    @GetMapping("/inventorys")
    @ResponseBody
    public Object allInventory(HttpServletRequest req) {
        System.out.println("接到请求");
        String proId  = req.getParameter("proId").trim();
        String proTpy = req.getParameter("proTpy").trim();
        String stoId = req.getParameter("stoId").trim();
        Inventory inventory = new Inventory();
        Product product = new Product();
        if (proId != "") {
            product.setId(Integer.parseInt(proId));
        }
        if(proTpy != "") {
            Type type = new Type();
            type.setId(Integer.parseInt(proTpy));
            product.setType(type);
        }
        inventory.setProduct(product);
        if (stoId != "") {
            Store store = new Store();
            store.setId(Integer.parseInt(stoId));
            inventory.setStore(store);
        }
        List<Inventory> res = this.inventoryService.allInventory(inventory);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }

    // 增加 库存管理
    @PostMapping("inventorys/")
    @ResponseBody
    public Object addInventory(@RequestBody Inventory inventory) {

        Date now = new Date();

        inventory.setCreateTime(now);
        inventory.setUpdateTime(now);
        System.out.println(inventory);

        boolean res = inventoryService.addInventory(inventory);

        if (res) {
            return ResUtils.Success(1,"添加成功",inventory);
        } else {
            return ResUtils.Fail(0,"添加失败");
        }
    }

    // 获取库存列表数据
    @PostMapping("/inventorys/store")
    @ResponseBody
    public Object inventoryOfStore(@RequestBody Inventory inventory) {

        System.out.println(inventory);
        List<Inventory> res = this.inventoryService.inventoryOfStore(inventory);
        System.out.println(res);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }


}

